package org.txlcn.demo;

import com.codingapi.txlcn.common.util.Transactions;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.codingapi.txlcn.tracing.TracingContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.txlcn.demo.common.db.domain.Stock;

import java.util.Date;

/**
 * Description:
 * Date: 2018/12/25
 *
 * @author ujued
 */
@Service
@Slf4j
public class StockServiceImpl implements StockService {

    private final StockMapper demoMapper;

    @Autowired
    public StockServiceImpl(StockMapper demoMapper) {
        this.demoMapper = demoMapper;
    }

    @Override
    @LcnTransaction
    public String rpc(String value) {

        Stock stock = new Stock();
        stock.setGroupId(TracingContext.tracing().groupId());
        stock.setDemoField(value);
        stock.setAppName(Transactions.getApplicationId());
        stock.setCreateTime(new Date());
        demoMapper.saveStock(stock);

        if ("iii".equals(value)) {
            try {
                Thread.sleep(3000);
                throw new RuntimeException("Order save failed");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "ok-service-b";
    }
}
